package com.easepal7506a.project.ui.activity;

import android.os.Handler;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.easepal7506a.project.Constant.CommmandNum;
import com.easepal7506a.project.R;
import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.observer.CloseAdvance;
import com.easepal7506a.project.observer.CloseScanAndAdjust;
import com.easepal7506a.project.observer.TimeObserver;
import com.easepal7506a.project.ui.fragment.AirPressureMassageFragment;
import com.easepal7506a.project.ui.fragment.FragmentAdapter;
import com.easepal7506a.project.ui.fragment.MassageTechniqueFragment;
import com.easepal7506a.project.ui.iview.IAdvancedMassageView7506;
import com.easepal7506a.project.ui.presenter.AdvancedMassagePressenter7506;
import com.easepal7506a.project.utils.ActivityUtils;
import com.easepal7506a.project.widget.CustomViewPager;
import com.ogawa.base.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 高级按摩
 */
public class AdvancedMassageActivity7506 extends BaseActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener,IAdvancedMassageView7506,
        CloseAdvance.onTimeFinish,TimeObserver.onTimeUpdataLis {


    public static final int TAG_ANMOSHOUFA = 0;
    public static final int TAG_QIYAANMO = 1;
    /**
     * 存放内容页面
     */
    private ArrayList<Fragment> contentPage;
    private RadioButton tableft;
    private RadioButton tabright;
    private ImageView mIvStart;
    private Fragment mFrag;
    private ImageView mIvPower;
    private String model;
    private RelativeLayout mRlTop;
    private FragmentAdapter mAdapter;//适配器
    private List<String> mTabs = new ArrayList<>();//存放标签页标题

    private AdvancedMassagePressenter7506 mPresenter;
    private MassageTechniqueFragment mFrangment1;
    private AirPressureMassageFragment mFrangment2;
    private CustomViewPager viewPager;
    private boolean isFinishShow;
    private boolean isPowerOn;
    private String mMin,mSec;
    private Handler handler;
    @Override
    public void initTitleBar() {
        super.initTitleBar();
        showTitleBar(false);
        setBackGround(R.mipmap.bg);

    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        handler = new Handler();
        initFragment();
        initView();
        iniTitle();
        mPresenter = new AdvancedMassagePressenter7506(this);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPresenter.getTimes();
            }
        },500);

        CloseAdvance.getInst().registerObserver(this);
        TimeObserver.getInst().registerObserver(this);
    }




    private void initFragment() {
        contentPage = new ArrayList<Fragment>();
        mFrangment1 = new MassageTechniqueFragment();
        mFrangment2 = new AirPressureMassageFragment();

        contentPage.add(mFrangment1);
        contentPage.add(mFrangment2);
        mTabs.add("按摩手法");
        mTabs.add("气压按摩");


    }

    private void iniTitle() {
        mRlTop = findViewById(R.id.re);
        CommonUtil.setMargins(mRlTop,0, CommonUtil.getStatusBarHeight(this),0,0);
        findViewById(R.id.chair_back).setOnClickListener(this);
        mIvPower =  findViewById(R.id.chair_power_close);
        mIvPower.setOnClickListener(this);
        mIvPower.setSelected(DataManager.getInst().isOpen());
    }


    private void initView() {

        model = getIntent().getStringExtra("model");
        viewPager =   findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(1);
        mAdapter = new FragmentAdapter(getSupportFragmentManager(), contentPage, mTabs);
        viewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        viewPager.setOnViewPagerScrollAction(new CustomViewPager.setOnViewPagerScrollAction() {
            @Override
            public boolean unAbleScroll() {
                return true;
            }
        });
        tableft =  findViewById(R.id.tableft);
        tabright =  findViewById(R.id.tabright);

        isPowerOn = DataManager.getInst().isOpen();
        tableft.setOnCheckedChangeListener(this);
        tableft.setTag(TAG_ANMOSHOUFA);
        tableft.setChecked(true);


        tabright.setOnCheckedChangeListener(this);
        tabright.setTag(TAG_QIYAANMO);

        mIvStart = findViewById(R.id.iv_switch);
        Log.e("running",(DataManager.getInst().getValue(CommmandNum.mStatusRunning)+""));
        if(DataManager.getInst().getValue(CommmandNum.mStatusRunning).equals("3")){
            mIvStart.setSelected(true);
        }else {
            mIvStart.setSelected(false);
        }
        mIvStart.setOnClickListener(this);
        if(DataManager.getInst().getValue(CommmandNum.mStatusShoulderScanning).equals("1")){
            ActivityUtils.startScanShoulderActivity(this);
        }else if(DataManager.getInst().getValue(CommmandNum.mStatusShoulderAdjust).equals("1")){
            ActivityUtils.startShouldersAdjustmentActivity(this);

        }

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.chair_back) {
            finish();

        } else if (i == R.id.iv_switch) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.PAUSE);
            }

        } else if (i == R.id.chair_power_close) {
            if(isPowerOn){
                sendCommond(CommmandNum.CLOSE);
            }
            else {
                sendCommond(CommmandNum.OPEN);
            }
        }
    }





    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {
            int i = buttonView.getId();
            if (i == R.id.tableft) {
                viewPager.setCurrentItem(0);
                tableft.setBackgroundResource(R.mipmap.tab_letf);
                tabright.setBackgroundResource(0);

            } else if (i == R.id.tabright) {
                tabright.setBackgroundResource(R.mipmap.tab_letf);
                tableft.setBackgroundResource(0);
                viewPager.setCurrentItem(1);

            } else {
            }

        }
    }



    @Override
    public int setLayoutId() {
        return R.layout.activity_advanced_massage_7506;
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestory();
        CloseAdvance.getInst().removeObserver(this);
        TimeObserver.getInst().removeObserver(this);
    }

    @Override
    public void isPause(boolean isPause) {
        mIvStart.setSelected(isPause);
        if(isPause){
            mFrangment1.pauseTime();
            mFrangment2.pauseTime();
        }
        else {
            mFrangment1.reStarTime();
            mFrangment2.reStarTime();

        }
    }

    @Override
    public void getTime(String min, String sec) {
        if(!(min.equals("0")&&sec.equals("0"))){
            mFrangment1.initTime(min,sec);
            mFrangment2.initTime(min,sec);
        }

    }

    @Override
    public void toAutoActivity(String name) {
        cancleFinishDialog();
        CloseScanAndAdjust.getInst().noticeonClose();
        if(DataManager.getInst().isIs7506L()){
            ActivityUtils.startProMasDetailActivityL(this,name);
        }
        else {
            ActivityUtils.startProMasDetailActivity(this,name);
        }

        this.finish();
    }


    @Override
    public void startToScan() {
        ActivityUtils.startScanShoulderActivity(this);

    }

    @Override
    public void startAdjust() {
        ActivityUtils.startShouldersAdjustmentActivity(this);
    }

    @Override
    public void handRunning() {
        cancleFinishDialog();
    }


    @Override
    public void onTimeFinish() {
        sendCommond(CommmandNum.FINISH);
        Log.e("onTimeFinish",isFinishDialogShowing()+"");
        if(!isFinishShow){
            isFinishShow = true;
            showFinishDialog(this,"-1", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isFinishShow = false;
                    cancleFinishDialog();
                }
            });
        }


    }

    @Override
    public void onupdataTime(String min, String sec) {
        mMin = min;
        mSec = sec;
        mFrangment1.upDataTime(min,sec);
        mFrangment2.upDataTime(min,sec);
    }
}
